package com.rxjavademo.presenter;

import android.provider.SyncStateContract;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.rxjavademo.api.Api;
import com.rxjavademo.base.BaseActivity;
import com.rxjavademo.base.BasePresenter;
import com.rxjavademo.bean.HomeBean;
import com.rxjavademo.common.Constant;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
//https://www.jianshu.com/p/1a5f14b63f47   同步持久Cookie Retrofix2.0+Rxjava

/**
 * Created by robert.luozizhao on 2018/1/17.
 */

public class MainActivityPresenter<V extends BaseActivity> extends BasePresenter {


    private static final String TAG = "MainActivityPresenter";
    private HttpTransportSE header;

    private String hearInfo = "";

    /**
     * 绑定view
     *
     * @param view
     */
    public MainActivityPresenter(V view) {
        attachView(view);
    }

    public void queryHome() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.HOST)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        Api api = retrofit.create(Api.class);
        Observable<HomeBean> homeBeanObservable = api.queryHome();

        homeBeanObservable
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe: ");
                    }

                    @Override
                    public void onNext(HomeBean bean) {
                        Log.d(TAG, "onNext: ");
                        if (bean == null) {
                            Log.d(TAG, "onNext: bean=null");
                            return;
                        }
                        List<HomeBean.HomeTopicBean> homeTopicBeanList = bean.getHomeTopic();
                        if (homeTopicBeanList != null) {
                            for (int i = 0; i < homeTopicBeanList.size(); i++) {
                                Log.d(TAG, "onNext: item" + homeTopicBeanList.get(i).toString());
                            }
                        }

                        Log.d(TAG, "onNext: bean.tostring:" + bean.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: e:" + e.toString() + ",messaged:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete: ");
                    }

                });
    }

    public void textQuery() {
        //1 创建HttpTransportSE传输对象
        final HttpTransportSE transport = new HttpTransportSE(Constant.WEB_URL, Constant.TIMEOUT);

        //2 设置SOAP协议的版本号，根据服务端WebService的版本号设置。
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        soapEnvelope.dotNet = true;// 设置与.NET提供的webservice保持较好的兼容性

        //3 第一个参数表示WebService的命名空间，可以从WSDL文档中找到WebService的命名空间。
        // 第二个参数表示要调用的WebService方法名。
        SoapObject request = new SoapObject(Constant.WEB_NAMESPACE, Constant.METHOD);

        //4 记得设置bodyout属性 envelope.bodyOut = soapObject;
        soapEnvelope.bodyOut = request;
        soapEnvelope.setOutputSoapObject(request); //传入实例化SoapObject --》 request

        //请求参数
        request.addProperty("account", "admin");
        request.addProperty("passWord", "123456");
        request.addProperty("terminalId", "358537040046723");

        SoapPrimitive response=null;

        try {
            if (TextUtils.isEmpty(hearInfo)) {
                List<HeaderProperty> respHeaders = null;
                respHeaders = transport.call("", soapEnvelope, null);
                for (HeaderProperty hp : respHeaders) {
                    String key = hp.getKey();
                    if (key != null) {
                        if (key.equals("Set-Cookie")) {
                            hearInfo = hp.getValue();
                        }
                    }
                }
            } else {
                List<HeaderProperty> headers=new ArrayList<HeaderProperty>();
                headers.add(new HeaderProperty("Cookie",hearInfo));
                transport.call("",soapEnvelope,headers);
            }
            response = (SoapPrimitive) soapEnvelope.getResponse();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }


    }


}
